package com.cgw.common.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.cgw.common.component.springhelper.BeanLocator;
import com.cgw.dmplt.common.BaseDomain;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction  extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected Log log=LogFactory.getLog(this.getClass());
	/**
	 * error msg.
	 */
	private static final String	ERROR_MSG			= "msg";

	/**
	 * error stack.
	 */
	private static final String	ERROR_STACK			= "errorStack";

	/**
	 * 缺省页行数.
	 */
	private static final int	DEFAULT_PAGE_LIMIT	= 9999;
	/**
	 * action执行结果（true|flase）.
	 */
	private boolean				success				= true;

	/**
	 * action执行结果（json插件返回客户端）.
	 */
	private Map					result				= null;

	/**
	 * action执行结果原因（json插件返回客户端）.
	 */
	private Map					errorReason			= new HashMap();

	/**
	 * 起始行号.
	 */
	private int					start				= 0;

	/**
	 * 行数.
	 */
	private int					limit				= DEFAULT_PAGE_LIMIT;

	/**
	 * 返回总行数.
	 */
	private int					totalCount			= 0;

	/**
	 * 排序字段（例sp.spCode）.
	 */
	private String				sort;

	/**
	 * 正序|反序（例ASC）.
	 */
	private String				dir;
	
	
	
//	@JSON(name = "data")
//	public Map getResult() {
//		return result;
//	}

	public void setResult(Map result) {
		this.result = result;
	}

	/**
	 * 设置输出接果.
	 *
	 * @param key
	 *            键
	 * @param value
	 *            值
	 */
	public void setResult(Object key, Object value) {
		if (result == null) {
			result = new HashMap();
		}
		result.put(key, value);
	}
	
	
	public final Object getBean(String key) {
		return BeanLocator.getInstance().getBean(key);
	}
	
	/**
	 * 得到http session.
	 *
	 * @return http session
	 */
	public final HttpSession getSession() {
		return getHttpServletRequest().getSession(true);
	}

	/**
	 * 得到http request.
	 *
	 * @return http request
	 */
	public final HttpServletRequest getHttpServletRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 得到http session.
	 *
	 * @return http request
	 */
	public final HttpSession getHttpSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 得到http response.
	 *
	 * @return http response
	 */
	public final HttpServletResponse getHttpServletResponse() {
		return ServletActionContext.getResponse();
	}
	
	
	/**
	 * 设置分页属性.
	 *
	 * @param domain
	 *            输入DO对象
	 */
	public void setPagination(BaseDomain domain) {
		// 设置分页属性: start,limit,sort,order
		domain.setStart(getStart() + 1);
		domain.setEnd(getStart() + getLimit());
		domain.setSort(getSort());
		domain.setOrder(getDir());
	}

	public int getStart() {
		return this.start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return this.limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getSort() {
		return this.sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return this.dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}
}
